﻿using Artech.Common;
using Microsoft.Practices.Unity.Utility;
using System;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.ServiceModel;
using System.Text;
using System.Threading.Tasks;

namespace Artech.Core.Resources
{
    [ServiceBehavior(InstanceContextMode = InstanceContextMode.PerCall)]
    public class ResourceService : IResourceService
    {
        public ResourceProviderBase Provider
        {
            get { return (ResourceProviderBase)ResourceManager.Current.Provider; }
        }

        public StringResourceEntry[] GetStringsForEdit(StringResourceSearchCriteria criteria, int pageSize, int pageIndex, ResourceEntryField orderBy, Common.SortDirection sortDirection)
        {
            Guard.ArgumentNotNull(criteria, "criteria");
            Guard2.ArgumentIsValidePageSize(pageSize, "pageSize");
            Guard2.ArgumentIsValidePageIndex(pageIndex, "pageIndex");
            Guard.ArgumentNotNull(orderBy, "orderBy");
            return this.Provider.GetStringsForEdit(criteria, pageSize, pageIndex, orderBy, sortDirection).ToArray();
        }

        public void UpdateStrings(StringResourceEntry[] resourceEntries)
        {
            Guard.ArgumentNotNull(resourceEntries, "resourceEntries");
            this.Provider.UpdateStrings(new StatefulEntryCollection<StringResourceEntry>(resourceEntries));
        }

        public StringResourceItem[] GetAllStrings(string culture)
        {
            Guard.ArgumentNotNullOrEmpty(culture, "culture");
            return this.Provider.GetStringsCore( new CultureInfo(culture)).ToArray();
        }

        public StringResourceItem[] GetStringsByCategory(string category, string culture)
        {
            Guard.ArgumentNotNullOrEmpty(category, "category");
            Guard.ArgumentNotNullOrEmpty(culture, "culture");
            return this.Provider.GetStringsCore(category, new CultureInfo(culture)).ToArray();
        }

        public StringResourceItem[] GetStringsByName(string category, string name, string culture)
        {
            Guard.ArgumentNotNullOrEmpty(category, "category");
            Guard.ArgumentNotNullOrEmpty(name, "name");
            Guard.ArgumentNotNullOrEmpty(culture, "culture");
            return this.Provider.GetStringsCore(category, name, new CultureInfo(culture)).ToArray();
        }
    }
}